import { HashRouter, Switch, Route, Redirect, withRouter } from 'react-router-dom';

let unique = (arr) => {
  const res = new Map();
  return arr.filter((arr) => !res.has(arr.path) && res.set(arr.path, 1))
}
export {
  HashRouter,
  withRouter,
  Switch, 
  Route, 
  Redirect,
}
class BaseRouter{
  constructor(props){
    let {routes, redirect} = props;
    // 路由去重
    this.routes = unique(routes)
    this.redirect = redirect
    return this.init()
  }
  init() {
    let list = this.routes.map((item, index) => {
      return ( 
        <Route
          key={index}
          exact={item.exact}
          path={item.path}
          render={() => <item.component route={item.children} />}
        />
      );
    })
    return (
      <Switch>
        {list}
        {
          this.redirect?<Redirect to={this.redirect}></Redirect>:null
        }
      </Switch>
    );
  }
}
export default BaseRouter